package com.huawei.admins.platform.gateway.core.component.predicate;

import org.reactivestreams.Publisher;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;

public class NegateAsyncPredicate <T> implements AsyncPredicate<T> {

    private final AsyncPredicate<? super T> predicate;

    public NegateAsyncPredicate(AsyncPredicate<? super T> predicate) {
        Assert.notNull(predicate, "predicate AsyncPredicate must not be null");
        this.predicate = predicate;
    }

    @Override
    public Publisher<Boolean> apply(T t) {
        return Mono.from(predicate.apply(t)).map(b -> !b);
    }

    @Override
    public String toString() {
        return String.format("!(%s)", this.predicate);
    }

}